package com.ruoyi.system.service.aggregation.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.aggregation.Order;
import com.ruoyi.system.domain.aggregation.User;
import com.ruoyi.system.mapper.aggregation.OrderMapper;
import com.ruoyi.system.mapper.aggregation.UserMapper;
import com.ruoyi.system.mapper.aggregation.AccountMapper;
import com.ruoyi.system.service.aggregation.IStatisticsService;

/**
 * 数据统计Service实现
 * 
 * @author ruoyi
 * @date 2025-01-01
 */
@Service
public class StatisticsServiceImpl implements IStatisticsService 
{
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private OrderMapper orderMapper;
    
    @Autowired
    private AccountMapper accountMapper;

    /**
     * 首页统计数据
     */
    @Override
    public Map<String, Object> getHomeStatistics()
    {
        Map<String, Object> result = new HashMap<>();

        // 使用 COUNT 查询统计各类型用户数量（性能优化）
        int cUserCount = userMapper.countUserByType(1);       // C端用户
        int salesmanCount = userMapper.countUserByType(2);    // 业务员
        int merchantCount = userMapper.countUserByType(3);    // 商家
        int regionCount = userMapper.countUserByType(4);      // 区域代理

        result.put("cUserCount", cUserCount);
        result.put("salesmanCount", salesmanCount);
        result.put("merchantCount", merchantCount);
        result.put("regionCount", regionCount);

        // 使用 COUNT 查询统计订单（性能优化）
        int totalOrders = orderMapper.countOrderByStatue(null);  // 所有订单
        int paidOrders = orderMapper.countOrderByStatue(2);      // 已支付订单

        result.put("totalOrders", totalOrders);
        result.put("paidOrders", paidOrders);

        return result;
    }

    /**
     * 贡献值统计（区域代理维度）
     */
    @Override
    public Map<String, Object> getContributionStatistics()
    {
        Map<String, Object> result = new HashMap<>();

        // 获取所有区域代理（只需要ID和名称）
        User query = new User();
        query.setType(4); // 区域代理
        List<User> regions = userMapper.selectUserList(query);

        List<String> regionNames = new ArrayList<>();
        List<Integer> merchantCounts = new ArrayList<>();
        List<Integer> salesmanCounts = new ArrayList<>();

        for (User region : regions)
        {
            regionNames.add(region.getName());

            // 使用 COUNT 查询统计该区域的商家数量（性能优化）
            int merchantCount = userMapper.countUserByTypeAndRegion(3, region.getId());
            merchantCounts.add(merchantCount);

            // 使用 COUNT 查询统计该区域的业务员数量（性能优化）
            int salesmanCount = userMapper.countUserByTypeAndRegion(2, region.getId());
            salesmanCounts.add(salesmanCount);
        }

        result.put("regionNames", regionNames);
        result.put("merchantCounts", merchantCounts);
        result.put("salesmanCounts", salesmanCounts);

        return result;
    }

    /**
     * 订单统计
     */
    @Override
    public Map<String, Object> getOrderStatistics()
    {
        Map<String, Object> result = new HashMap<>();

        // 使用 COUNT 和 SUM 聚合查询（性能优化）
        int totalOrders = orderMapper.countOrderByStatue(null);
        Long totalAmount = orderMapper.sumOrderAmount();
        Long totalPayAmount = orderMapper.sumOrderPayAmount();

        result.put("totalOrders", totalOrders);
        result.put("totalAmount", totalAmount != null ? totalAmount : 0L);
        result.put("totalPayAmount", totalPayAmount != null ? totalPayAmount : 0L);

        return result;
    }

    /**
     * 分佣统计
     */
    @Override
    public Map<String, Object> getCommissionStatistics()
    {
        Map<String, Object> result = new HashMap<>();

        // 使用 SUM 聚合查询统计各角色的分佣总额（性能优化）
        Long merchantCommission = accountMapper.sumTotalBalanceByUserType(3);  // 商家
        Long salesmanCommission = accountMapper.sumTotalBalanceByUserType(2);  // 业务员
        Long regionCommission = accountMapper.sumTotalBalanceByUserType(4);    // 区域代理
        Long companyCommission = accountMapper.sumTotalBalanceByUserType(5);   // 总公司
        
        long total = (merchantCommission != null ? merchantCommission : 0L)
                   + (salesmanCommission != null ? salesmanCommission : 0L)
                   + (regionCommission != null ? regionCommission : 0L)
                   + (companyCommission != null ? companyCommission : 0L);

        result.put("totalCommission", total);
        result.put("merchantCommission", merchantCommission != null ? merchantCommission : 0L);
        result.put("salesmanCommission", salesmanCommission != null ? salesmanCommission : 0L);
        result.put("regionCommission", regionCommission != null ? regionCommission : 0L);
        result.put("companyCommission", companyCommission != null ? companyCommission : 0L);

        return result;
    }
}

